﻿using Parsers;
using ScreenCapture;
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using System.Drawing;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace TCPServer
{
    public class Servers
    {
        static int DefaultPort = 55665;
        private Thread serverThread;
        private bool running;
        private IPAddress hostIP;
        private int port;

        public Servers()
        {
            Running = false;
            hostIP = IPAddress.Any;
            this.Port = Servers.DefaultPort;
        }

        public Servers(int serverPort)
        {
            Running = false;
            hostIP = IPAddress.Any;
            this.Port = serverPort;
        }

        public int Port 
        { 
            get 
            {
                return port;
            }
            set
            {
                if (Running)
                {
                    throw new InvalidOperationException("Not allowed once the server has started");
                }
                if (value >= IPEndPoint.MinPort && value <= IPEndPoint.MaxPort)
                {
                    port = value;
                }
                else
                {
                    throw new ArgumentOutOfRangeException("Port should be in between 0 to 65536");
                }
            }
        }

        public IPAddress HostIP 
        { 
            get
            {
                return hostIP;
            }
            set 
            {
                if (Running)
                {
                    throw new InvalidOperationException("Not allowed once the server has started");
                }

                hostIP = value;
            }
        }

        public bool Running
        {
            get { return running; }
            protected set
            {
                running = value;
            }
        }

        
        public void Start()
        {
            serverThread = new Thread(new ThreadStart(Serve));
            serverThread.Start();
        }

        public bool Stop()
        {
            Running = false;
            serverThread.Interrupt();
            return Running;
        }

        public virtual void Serve()
        {
            //if (Running)
            //{
            //    return;
            //}
            //else
            //{
            //    Running = true;
            //}

            //host = (EndPoint)new IPEndPoint(IPAddress.Any, 55665); // get host end point | listen to all interfaces | Port: 55665
            //Socket listener;
            //listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //listener.Bind(host);    // bind to host
            //listener.Listen(10);
            //while (Running)
            //{
            //    StringBuilder msg = new StringBuilder();
            //    try
            //    {
            //        Socket conn = listener.Accept();
            //        if (conn != null)
            //        {
            //            // Do something
            //            // Call some function with socket as parameter
            //        }
            //    }
            //    catch (ThreadInterruptedException tex)
            //    {
            //        tex.ToString();
            //        listener.Close();
            //        break;  // stop thread
            //    }
            //    catch (ThreadAbortException tex)
            //    {
            //        tex.ToString();
            //        listener.Shutdown(SocketShutdown.Both);
            //        break;  // stop thread
            //    }
            //    catch (Exception ex)
            //    {
            //        ex.ToString();  //do something
            //    }
            //    Console.WriteLine("Shutting down");
            //}
        }

        //public virtual void Serve();
    }
}
